<?php
namespace app\app\controller\v2;
use app\app\Appbase;

class Order extends Appbase 
{
	/**
	* 订单列表
	* /app/v2/order/getList
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	public function getList($name='',$pageIndex=1,$pageSize=20){
		$param = request()->get();
		$where[] = ['shop_id', '=', request()->shopId ];
		if(!empty($name)){
			$where[] = ['order_no', 'like', "%$name%"];
		}
		if($param['status'] == -1){
			$where[] = ['refund_status', '<>', '0'];
		}else if(trim($param['status']) != ''){
			$where[] = ['status', '=', $param['status']];
			$where[] = ['refund_status', 'in', '0,1'];
		}
		if(!empty($param['is_pay'])){
			$where[] = ['is_pay', 'in', $param['is_pay']];
		}
		try {
			$result = model('order')->getList($where,$pageIndex,$pageSize);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取订单明细
	* /app/v2/order/details
	* 需要登录 
	* @is_need_login true
	* @method all
	*/
	public function details($id){
		if(!$id){
			return $this->ReError('订单id不能为空！');
		}
		try {
			$result = model('order')->details($id);
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 发货
	* /app/v2/order/send
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function send($id){
		$request =  request();
		$param = request() ->post();
		if(!$id){
			return $this->ReError('订单id不能为空！');
		}
		try {
			$model = model('order');
			$result = $model->send($param);
			if($result === false){
				return $this->ReError( $model->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 退款
	* /app/v2/order/refund
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function refund($id){
		if(!$id){
			return $this->ReError('订单id不能为空！');
		}
		try {
			$model = model('order');
			$result = $model->refund($id);
			if($result === false){
				return $this->ReError( $model->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 订单退款驳回
	* /app/v2/order/refundReject
	* 需要登录 
	* @is_need_login true
	*/ 
    public function refundReject($id,$desc='')
    {
    	if(!$id) return $this->ReError('订单id不能为空！');
    	if(empty($desc)) return $this->ReError('原因不能为空！');
		try {
			$model = model('order');
			$result = $model->rejectApply($id,$desc);
			if($result === false){
				return $this->ReError( $model->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
    }
	
	/**
	* 订单评论
	* /app/v2/order/comment
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function comment($id){
		if(!$id){
			return $this->ReError('订单id不能为空！');
		}
		try {
			$model = model('order');
			$result = $model->comment($id);
			if($result === false){
				return $this->ReError( $model->errorInfo );
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



